<?php

class Coachmodel extends CI_Model {

	function __construct()
    {
        parent::__construct();
	    $this->load->database();
    }

    function getCoachlist($param){
	 	
  	 	$pageno = $param['pageno'];
  	 	$stype = $param['stype'];
  	 	$areaid = $param['areaid'];
  	 	$sex = $param['sex'];
  	 	$sorttype = $param['sorttype'];

  	 	$pagesize = (int)$param['pagesize'];

  	 	$rs_number = ($pageno - 1) * $pagesize ;

  	 	$wherestr = ' where status=1 ';
  	 	if( $stype !='0' && $stype!=''&& $stype!='all'){
  	 		$wherestr = $wherestr.' and stype_main='.$stype ; 
  	 	}
  	 	if( $areaid !='0' && $areaid!=''&& $areaid!='all'){
  	 		$wherestr = $wherestr.' and areaid='.$areaid ; 
  	 	}
  	 	if( $sex !='0' && $sex!=''&& $sex!='all'){
  	 		$wherestr = $wherestr.' and sex='.$sex ; 
  	 	}

	    $query = $this->db->query('SELECT * FROM coach '.$wherestr.' order by id desc limit ?,?' , array($rs_number, $pagesize));

       //$sql = $this->db->last_query();
       //echo($sql);
 
  		$res =  $query->result();

       foreach($res as $item){
          	 //头像处理
          	 if($item->thumb == ''){
          	 	$item->thumb = '/images/icon/user.png';
          	 }
          	 //主教类型
          	 $item->stypename = $this->getStypename($item->stype_main);
          	 //价格处理
          	 if($item->stype_main_price == '0'){
          	 	 $item->stype_main_price = '-';
          	 }
             if($item->areaname == ''){
               $item->areaname = '深圳';
             }

             $item->age = $this->getAge($item->birthday);
       }

        return $res;
    }


    function getCoach($param){
        $coachid = $param['id'];
        $query = $this->db->query("select * FROM coach where id=?",array($coachid));
    		$res =  $query->row();

    		$res->sexname = $this->getSexname($res->sex);
     		$res->age = $this->getAge($res->birthday);

      	return $res;
    }

    public function getSexname($sex){
      	if($sex==1){
      		return '男'; 
      	}
      	if($sex==2){
      		return '女'; 
      	}
      	return '';
    }

    public function getAge($birthday){
      date_default_timezone_set("PRC");
 		  $age = date('Y', time()) - date('Y', strtotime($birthday)) - 1;
		  
		  if (date('m', time()) == date('m', strtotime($birthday))){
 				if (date('d', time()) > date('d', strtotime($birthday))){
						$age++;
				  }
		   }elseif (date('m', time()) > date('m', strtotime($birthday))){
				$age++;
		   }
		   return $age;
    }


   

    public function getStypename($stypeid){
  		$sname = '';
  		if($stypeid=='1'){
  		    	$sname = '健身';
   		}
  		    if($stypeid=='2'){
  		    	$sname = '羽毛球';
   		    }
  		    if($stypeid=='3'){
  		    	$sname = '网球';
   		    }
  		    if($stypeid=='4'){
  		    	$sname = '高尔夫';
   		    }
  		    if($stypeid=='5'){
  		    	$sname = '游泳';
   		    }
  		    if($stypeid=='6'){
  		    	$sname = '瑜伽';
   		    }
  		    if($stypeid=='7'){
  		    	$sname = '跑步';
   		    }
          if($stypeid=='8'){
            $sname = '兵乓球';
          }

   		return $sname;
   	}


   public function getStypebg($stypeid){
       $sname = '/images/stype/run.jpg';
      if($stypeid=='1'){
            $sname = '/images/stype/s1.jpg';
      }
          if($stypeid=='2'){
            $sname = '/images/stype/s2.jpg';
          }
          if($stypeid=='3'){
            $sname = '/images/stype/s3.jpg';
          }
          if($stypeid=='4'){
            $sname = '/images/stype/s4.jpg';
          }
          if($stypeid=='5'){
            $sname = '/images/stype/s5.jpg';
          }
          if($stypeid=='6'){
            $sname = '/images/stype/s6.jpg';
          }
          if($stypeid=='7'){
            $sname = '/images/stype/s7.jpg';
          }
          if($stypeid=='8'){
            $sname = '/images/stype/s8.jpg';
          }

      return $sname;
    }


}
?>